package service;

import domain.Account;
import persistence.AccountDao;
import persistence.impl.AccountDAOImpl;

public class AccountService {

    private AccountDao accountDao;

    public AccountService(){
        this.accountDao = new AccountDAOImpl();
    }

    //通过用户名获取账户信息
    public Account getAccount(String username){
        return accountDao.getAccountByUsername(username);
    }

    //通过用户名和密码获取账户信息
    public Account getAccount(String username,String password){
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        return accountDao.getAccountByUsernameAndPassword(account);
    }

    //插入账户
    public void insertAccount(Account account){
        accountDao.insertAccount(account);
        accountDao.insertProfile(account);
        accountDao.insertSignon(account);
    }

    //更新账户
    public void updateAccount(Account account){
        accountDao.updateAccount(account);
        accountDao.updateProfile(account);
        account.setBannerName(accountDao.getBannerNameByFavoriteCategory(account.getFavouriteCategoryId()));

        if (account.getPassword() != null&& account.getPassword().length() > 0){
            accountDao.updateSignon(account);
        }
    }

}
